faApp.controller('SupplierListCtrl',['$scope','supplierService','$location','$controller','$route','$routeParams',function($scope,supplierService,$location,$controller,$route,$routeParams) {
  
  $controller('BaseListCtrl', {$scope: $scope});
  $scope.list = [];
  $scope.loading = true;
  //// console.log('aa');

  $scope.$watch('config',function(){
    if ($scope.watchIsChange()){
      supplierService.supplierList($scope.config).then(function(json){
        $scope.list = json.list;
		    $scope.pager = json.pager;
		    $scope.loading = false;
      });
    }
  },true);
}]);

faApp.controller('SupplierFlowListCtrl',['$scope','supplierService','$location','$controller','$route','$routeParams',function($scope,supplierService,$location,$controller,$route,$routeParams) {
  
  $controller('BaseListCtrl', {$scope: $scope});
  $scope.list = [];
  $scope.loading = true;
  $scope.accessId = $routeParams.accessId;

  $scope.$watch('config',function(){
    if ($scope.watchIsChange()){
      
      $scope.config['accessId'] = $scope.accessId;

      supplierService.supplierFlowList($scope.config).then(function(json){
        $scope.list = json.list;
        $scope.pager = json.pager;
        $scope.loading = false;
      });
    }
  },true);
}]);

faApp.controller('SupplierAddCtrl',['$scope','supplierService','$location','$controller','$timeout',function($scope,supplierService,$location,$controller,$timeout) {   
  $controller('BaseCtrl', {$scope: $scope});

  $scope.item = {};
  $scope.loading = true;

  $scope.save = function(){
    if ($scope.form.$invalid){
      alert("请先完整填写表单");
      return;
    }else{
      if(($scope.item.accessMobile + "").length != 11){
        alert("手机号长度必须为11位！");
        return;
      }
      if(($scope.item.accessMobile + "").substr(0,1) != "1"){
        alert("手机号第一位必须是1!");
        return;
      }
      supplierService.supplierAdd($scope.item).then(function(json){
        if (json.statusCode){
          if(json.statusCode == '2003'){
            alert("供应方公司名重复！");
          }else{
            alert("出错了:" + json.statusCode);
          }
        }else{
          $scope.alertNextPage("操作成功");
          $location.path("/supplier/list");
        }
      });
    }
  };
}]);

faApp.controller('SupplierEditCtrl',['$scope','supplierService','$location','$controller','$routeParams','$filter',function($scope,supplierService,$location,$controller,$routeParams,$filter) {   
  $controller('BaseCtrl', {$scope: $scope}); 

  $scope.item = {};
  $scope.loading = true; 
  $scope.id = $routeParams.id;
  
  supplierService.supplierDetail({id:$scope.id}).then(function(json){
    $scope.loading = false;
    $scope.item = json['dto'];
    $scope.item.accessMobile = parseInt(json['dto'].accessMobile);
  });

  $scope.save = function(){
    if ($scope.form.$invalid){
      alert("请先完整填写表单");
      return;
    }else{

      supplierService.supplierEdit($scope.item).then(function(json){
        if (json.statusCode){
          if(json.statusCode == '2003'){
            alert("供应方公司名重复！");
          }else{
            alert("出错了:" + json.statusCode);
          }
        }else{
          $scope.alertNextPage("操作成功");
          $location.path("/supplier/list");
        }
      });
    }
  };
}]);